//
//  FCSettingsViewController.m
//  FengShuiCompass
//
//  Created by 张德广 on 2019/3/14.
//  Copyright © 2019 compass. All rights reserved.
//

#import "FCSettingsViewController.h"
#import <MessageUI/MessageUI.h>

@interface FCSettingsViewController () <UITableViewDelegate,UITableViewDataSource,MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) NSMutableArray *evaluteName;
@property (nonatomic, strong) NSMutableArray *mortgageAbout;
@property (nonatomic, strong) NSMutableArray *removeADName;

@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *cellNameArray;
@property (nonatomic, strong) NSMutableArray *cellImageArray;

@end

@implementation FCSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.view addSubview:self.bgImageView];
    [self.bgImageView addSubview:self.tableView];
    
    [self createVersionLabel];
    [self settingNavigationController];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}


#pragma mark ---------------------------------- versionView
- (void)createVersionLabel{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, FC_SCREEN_HEIGHT - 50, FC_SCREEN_WIDTH, 50)];
    view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 50)];
    label.center = CGPointMake(view.center.x, 25);
    label.textAlignment = NSTextAlignmentCenter;
    NSString *version = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    label.text = [NSString stringWithFormat:@"V %@",version] ;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor blackColor];
    label.alpha = 0.8;
    [view addSubview:label];
    
    [self.view addSubview:view];
}


#pragma mark ---------- 导航栏
-(void)settingNavigationController{
    
    self.title = NSLocalizedString(@"setting", nil);
    self.navigationController.navigationBar.tintColor = FCColorInRGB(0x000000);
}


#pragma mark ---------- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
        
        //设置cell的选中颜色
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    
    cell.imageView.image = [UIImage imageNamed:self.cellImageArray[indexPath.row]];
    [cell.textLabel setText:self.cellNameArray[indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70*kWidthValue;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
            [self evaluate];
            break;
        case 1:
            [self sendMessage];
            break;
        default:
            break;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark ---------------------------------- 点击cell执行的方法
- (void)sendMessage{
    
    if ([MFMailComposeViewController canSendMail] == YES) {
        
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        //  设置代理(与以往代理不同,不是"delegate",千万不能忘记呀,代理有3步)
        mailVC.mailComposeDelegate = self;
        //  收件人
        NSArray *sendToPerson = @[@"forrest3clemons4217@gmail.com"];
        [mailVC setToRecipients:sendToPerson];
        
        //  主题
        [mailVC setSubject:NSLocalizedString(@"Feedback", nil)];
        [self presentViewController:mailVC animated:YES completion:nil];
        [mailVC setMessageBody:@"" isHTML:NO];
    }else{
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"UnableSendMail", nil) message:NSLocalizedString(@"deviceNoOpen", nil) preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"sure", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                
                [[UIApplication sharedApplication] openURL:url];
            }
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"发送失败");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"保存草稿文件");
            break;
        case MFMailComposeResultSent:
            NSLog(@"发送成功");
            break;
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


//评价代理
- (void)evaluate{
    
    NSString *evaluateString = [NSString stringWithFormat:myAppID];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:evaluateString]];
}


#pragma mark ---------------------------------- 懒加载
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FC_SCREEN_WIDTH, FC_SCREEN_HEIGHT)];
        _bgImageView.image = [UIImage imageNamed:@"bgView"];
        _bgImageView.userInteractionEnabled = YES;
    }
    return _bgImageView;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, getRectNavAndStatusHight, FC_SCREEN_WIDTH, FC_SCREEN_HEIGHT- getRectNavAndStatusHight - 50) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70*kWidthValue;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return  _tableView;
}

- (NSMutableArray *)cellNameArray {
    if (!_cellNameArray) {
        _cellNameArray = [[NSMutableArray alloc] initWithObjects:
                          NSLocalizedString(@"Five-star praise", nil),
                          NSLocalizedString(@"Feedback", nil),
                          nil];
    }
    return _cellNameArray;
}

- (NSMutableArray *)cellImageArray {
    if (!_cellImageArray) {
        _cellImageArray = [[NSMutableArray alloc] initWithObjects:
                           @"advice",
                           @"pingjia",
                           nil];
    }
    return _cellImageArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
